﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class ConTractInfoDAL
    {

        public static List<MixedTaskCenterEntity> GetContract (MixedTaskCenterEntity model, int pageIndex, int pageSize, out int totalCount , string _statusStr)
        {
            DateTime? startTime = null;
            if (model.StartDateTime != DateTime.MinValue)
            {
                startTime = model.StartDateTime;
            }
            DateTime? endTime = null;
            if (model.EndDateTime != DateTime.MinValue)
            {
                endTime = model.EndDateTime;
            }
            var contractList = new List<MixedTaskCenterEntity>();
            var parameterOut = new SqlParameter()
            {
                ParameterName = "@TotalCount",
                SqlDbType = SqlDbType.BigInt,
                Direction = ParameterDirection.Output
            };
            SqlParameter[] searchParams = new SqlParameter[]{
                    new SqlParameter("Factory",model.Factory),
                    new SqlParameter("CreatedBy",model.CreatedBy),
                    new SqlParameter("ProcessType",model.ProcessType),
                    new SqlParameter("ForeignFactory",model.ForeignFactory),
                    new SqlParameter("ContractNo",model.ContractNo),
                    new SqlParameter("StartDateTime", startTime),
                    new SqlParameter("EndDateTime", endTime),
                    new SqlParameter("PageSize", pageSize),
                    new SqlParameter("PageIndex", pageIndex),
                    new SqlParameter("StatusStr",_statusStr),
                    parameterOut,
            };
            var dataTable = DBHelper.GetDataSet("USP_ContractView_getPage", searchParams);
            foreach (DataRow row in dataTable.Rows)
            {
                contractList.Add(new MixedTaskCenterEntity(row));
            }
            totalCount = Convert.ToInt32(parameterOut.Value);
            return contractList;

        }
    }
}
